package com.linwen.excise.base;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public  class Jisuanqi implements ActionListener,ItemListener {
	
	static JFrame f = null;
	ButtonGroup bg;
	JComboBox combo;
	JSpinner s1;
	JLabel L3;
	JRadioButton r1,r2,r3,r4;
	int op = 0;
	
	public Jisuanqi() {
		f = new JFrame("计算器");
		Container contentPane = f.getContentPane();
		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout(1,4));
		p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE,2)));
		
		r1 = new JRadioButton("+");
		r2 = new JRadioButton("-");
		r3 = new JRadioButton("*");
		r4 = new JRadioButton("/");
		p1.add(r1);
		p1.add(r2);	
		p1.add(r3);	
		p1.add(r4);	
		
		bg = new ButtonGroup();
		bg.add(r1);
		bg.add(r2);
		bg.add(r3);
		bg.add(r4);
		
		r1.addItemListener(this);
		r2.addItemListener(this);
		r3.addItemListener(this);
		r4.addItemListener(this);
		
		JPanel p2 = new JPanel();
		p2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.blue,2),"选择或输入操作数",TitledBorder.CENTER,TitledBorder.TOP));
		JLabel l1 = new JLabel("第一个操作数：");
		JLabel l2 = new JLabel("第二个操作数：");
		
		String data1 [] = {"0","10","20","30","40","50","60","70","80","90","100"};
		combo = new JComboBox(data1);
		combo.setEditable(true);
		ComboBoxEditor editor = combo.getEditor();
		combo.configureEditor(editor, "请选择或输入字母 ");
		SpinnerModel sM1 = new SpinnerNumberModel(50,0,100,1);
		s1 = new  JSpinner(sM1);
		
		p2.add(l1);
		p2.add(combo);
		p2.add(l2);
		p2.add(s1);
		JPanel p3 = new JPanel();
		p3.setLayout(new GridLayout(1,2));
		JButton button1 = new JButton("计算");
		L3 = new JLabel("",SwingConstants.CENTER);
		p3.add(button1);
		p3.add(L3);
		button1.addActionListener(this);
		
		contentPane.add(p1,BorderLayout.NORTH);
		contentPane.add(p2,BorderLayout.CENTER);
		contentPane.add(p3,BorderLayout.SOUTH);
		
		f.getRootPane().setDefaultButton(button1);
		f.pack();
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
			System.exit(0);}
		});
	}
		
	
	

	


	public void itemStateChanged(ItemEvent e) {
		if(e.getSource()==r1)
			op = 1;
		if(e.getSource()==r2)
			op = 2;
		if(e.getSource()==r3)
			op = 3;
		if(e.getSource()==r4)
			op = 4;
		
	}




	public void actionPerformed(ActionEvent arg0) {
		double a = Double.parseDouble(combo.getSelectedItem().toString());
		double b = Double.parseDouble(s1.getValue().toString());
		double c;
		switch (op) {
		case 1 :
			c= a + b;
			L3.setText(""+c);
			break;
		case 2 :
			c= a - b;
			L3.setText(""+c);
			break;
		case 3 :
			c= a * b;
			L3.setText(""+c);
			break;
		case 4 :
			c= a / b;
			L3.setText(""+c);
			break;
		default :
			L3.setText("选择运算符");
		
		
		}
		
	}
	
	public static void main(String[] args) {
		new Jisuanqi();

	}


}
